/*
* shrimp7 JS
* Copyright 2009 Rens Sparrius
*
* http://www.soepgarnaal.com/shrimp7/
* http://shrimp7.googlecode.com/
*
* Licensed under the BSD license.
*/

shrimp7.settings = shrimp7.fn.settings = {
	setupParams:{
		twitterInit:function(){
			shrimp7.twitter.init(TRUE);
		},
		facebookInit:function(){
			shrimp7.facebook.init(TRUE);
		}
	},
	setup:function(obj){
		for(var key in obj){
			this.setupParams[key] = obj[key];
		}
		return window.shrimp7;
	},
	starter:function(){
		var ThisContext = shrimp7.settings;
		
		setDimensions(400,500,'width');
		$("#flyoutContainer").height('auto');
		
		if(setting("language")!==""){
			ThisContext.setLanguages(setting("language"));
		}else{
			ThisContext.setLanguages('english');
		}
		
		ThisContext.loadSettings();
		
		ThisContext.setupPage('start',FALSE);
	},
	/*
	* settings.close(obj)
	*
	* optional:
	*	obj.openTab: string (twitter or facebook)
	*/
	close:function(obj){
		// Save some memory
		delete language.settings;
		
		if(config.system===sidebar){
			System.Gadget.Flyout.show = FALSE;
		}else{
			
		}
	},
	setLanguages:function(toLang){
		$("#settingsLanguageSelector option[value='"+toLang+"']",activeDom).attr("selected","selected");
		
		language.settings = window["shrimp7_language_settings_"+toLang]();
		
		$("*[class^=lngSettings]",activeDom).each(function(){
			var classDef = $(this,activeDom).attr("class").replace('.','');
			if(this.tagName.toLowerCase()==='input'){
				$(this,activeDom).val(language.settings[classDef]);
			}else{
				$(this,activeDom).html(language.settings[classDef]);
			}
		});
	},
	changeLanguage:function(){
		var toLanguage = $("#settingsLanguageSelector",activeDom).val();
		
		changeLanguage(toLanguage);
		
		this.setLanguages(toLanguage);
	},
	loadSettings:function(){
		if(setting("boolFacebook")){
			$('#boolFacebook',activeDom).attr(checked,checked);
		}else{
			$('#boolFacebook',activeDom).attr(checked,'');
		}
		
		$("#txtToken",activeDom).val(setting("facebookToken"));
		
		// Facebook sound
		if(setting("boolFacebookSound")){
			$('#boolFacebookSound',activeDom).attr(checked,checked);
		}else{
			$('#boolFacebookSound',activeDom).attr(checked,'');
		}
		
		if(setting("boolTwitter")){
			$('#boolTwitter',activeDom).attr(checked,checked);
		}else{
			$('#boolTwitter',activeDom).attr(checked,'');
		}
		
		$("#twitterUsername",activeDom).val(setting("twitterUsername"));
		$("#twitterPassword",activeDom).val(setting("twitterPassword"));
		
		if(setting("boolTwitterUseOauth")){
			$('#boolTwitterUseOauth',activeDom).attr(checked,checked);
		}else{
			$('#boolTwitterUseOauth',activeDom).attr(checked,'');
		}
		
		if(setting("twitterPIN")!==""){
			$("#twitterPIN",activeDom).val(setting("twitterPIN"));
		}
		
		// Als er al eens een code is opgevraagd..
		if(setting("twitterOauthToken")!==""){
			$(".twitterGetCodeLinkInText",activeDom).html('<a href="javascript:shrimp7.settings.twitterGetCode();">'+language.settings.lngSettingsTwitterRequestNewCodeExtra+'</a>.');
			$(".twitterGetCodeLink",activeDom).html('<a href="javascript:shrimp7.settings.twitterGetCode();">'+language.settings.lngSettingsTwitterRequestNewCode+'</a>');
		// Als er NOG GEEN code is opgevraagd..
		}else
			shrimp7.settings.twitterGetCode();
		
		if(setting("boolTwitterSound")){
			$('#boolTwitterSound',activeDom).attr(checked,checked);
		}else{
			$('#boolTwitterSound',activeDom).attr(checked,'');
		}
		
		if(setting("boolTwitterDeveloperAdded")){
			$('#boolTwitterDeveloper',activeDom).attr(checked,'');
		}else{
			$('#boolTwitterDeveloper',activeDom).attr(checked,checked);
		}
		
		/*
		* Twitter test tweet
		*
		* Set the checkbox to checked if there was never sended a test tweet.. else
		* set the checkbox to unchecked..
		*/
		
		if(setting("boolTwitterTestTweet")){
			$('#boolTwitterTestTweet',activeDom).attr(checked,'');
		}else{
			$('#boolTwitterTestTweet',activeDom).attr(checked,checked);
		}
		
		if(setting("boolFriendfeed")){
			$('#boolFriendfeed',activeDom).attr(checked,checked);
		}else{
			$('#boolFriendfeed',activeDom).attr(checked,'');
		}
		
		$("#friendfeedUsername",activeDom).val(setting("friendfeedUsername"));
		
		$("#friendfeedRemoteKey",activeDom).val(setting("friendfeedRemoteKey"));
	},
	twitterGetCode:function(){
		$(".twitterGetCodeLink,.twitterGetCodeLinkInText",activeDom).html('loading '+htmlImgIconsFront+'loading_dots.gif"alt=""style="vertical-align:middle;"/>');
		shrimp7.twitter.con.requestToken(function(link){
			$(".twitterGetCodeLink",activeDom).html('<a href="'+link+'"target="_blank">'+language.settings.lngSettingsTwitterGetTheCode+'</a>');
			$(".twitterGetCodeLinkInText",activeDom).html('<a href="'+link+'"target="_blank">'+language.settings.lngSettingsTwitterGetTheCodeDone+'</a>. '+language.settings.lngSettingsTwitterGetTheCodeDoneExtra);
		});
	},
	setupPage:function(page,second){
		var setupCancelButton = "#setupCancelButton",
			setupNextButton = "#setupNextButton";
		
		$(setupCancelButton,activeDom).show();
		$(setupNextButton).val(language.lngMainGadgetNext);
		
		$("*[id^='page']",activeDom).hide();
		$("#page_"+page,activeDom).show();
		
		if(page==='start'){
			$(setupCancelButton,activeDom).hide();
			$(setupNextButton).unbind(click).click(function(){shrimp7.settings.setupPage('selectServices',FALSE);});
		}
		if(page==='selectServices'){
			$(setupCancelButton).unbind(click).click(function(){shrimp7.settings.setupPage('start',FALSE)});
			$("#setupNextButton").unbind(click).click(function(){shrimp7.settings.setupPage('selectServices',TRUE)});
			
			if(second){
				// Write the values..
				setting("boolFacebook",$('#boolFacebook',activeDom).is(':checked'));
				setting("boolTwitter",$('#boolTwitter',activeDom).is(':checked'));
				setting("boolFriendfeed",$('#boolFriendfeed',activeDom).is(':checked'));
				if(!$('#boolFacebook',activeDom).is(':checked')&&!$('#boolTwitter',activeDom).is(':checked')&&!$('#boolFriendfeed',activeDom).is(':checked')){
					$("#error_selectServices",activeDom).html(language.settings.lngSettingsSelectServices).fadeIn();
				}else{
					if(setting("boolFacebook")){
						shrimp7.settings.setupPage('facebookCode',FALSE);
					}
					else if(setting("boolTwitter")){
						shrimp7.settings.setupPage('twitterCode',FALSE);
					}
					else if(setting("boolFriendfeed")){
					
					}
				}
			}
		}
		if(page==='facebookCode'){
			$(setupCancelButton).unbind(click).click(function(){shrimp7.settings.setupPage('selectServices',FALSE)}).show();
			$(setupNextButton).unbind(click).click(function(){shrimp7.settings.setupPage('facebookCode',TRUE)}).show();
			
			if(second){
				if($("#txtToken",activeDom).val()===""){
					$("#error_facebookCode",activeDom).html('Facebook one-time code is required').fadeIn();
				}else{
					shrimp7.settings.setupPage('facebookCheckCode',FALSE);
				}
			}
		}
		if(page==='facebookCheckCode'){
			$(setupCancelButton).unbind(click).click(function(){shrimp7.settings.setupPage('facebookCode',FALSE)}).hide();
			$(setupNextButton).unbind(click).click(function(){shrimp7.settings.setupPage('facebookSound',FALSE)}).hide();
			
			if(setting("facebookToken")!==$("#txtToken",activeDom).val()){
				$("#verifyingFacebookPIN").html(htmlImgIconsFront+'loading_dots.gif"alt=""/>');
				setting("facebookToken",$("#txtToken",activeDom).val());
				$("#facebookConnect",activeDom).show();
				// Get session key from Facebook server..
				shrimp7.facebook.con.getSessionKey(function(data){
					if(data!==errorTxt){
						if(data.error_code!==UNDEFINED){
							$(setupCancelButton).show();
							$("#verifyingFacebookPIN",activeDom).html(language.settings.lngSettingsTwitterVerifyingCodeError);
						}else{
							$(setupCancelButton+','+setupNextButton).show();
							setting("facebookUserId",data.uid);
							setting("SessionKey",data.session_key);
							$("#verifyingFacebookPIN",activeDom).html(language.settings.lngSettingsTwitterVerifyingCodeOK);
						}
					}else{
						$(setupCancelButton).show();
						$("#verifyingFacebookPIN",activeDom).html(language.settings.lngSettingsTwitterVerifyingCodeError);
					}
				});
			}else if(setting("SessionKey")!==""){
				$(setupCancelButton+','+setupNextButton).show();
				$("#verifyingFacebookPIN",activeDom).html(language.settings.lngSettingsTwitterVerifyingCodeOK);
				$("#facebookConnect",activeDom).show();
			}else{
				$(setupCancelButton).show();
				$("#verifyingFacebookPIN",activeDom).html(language.settings.lngSettingsTwitterVerifyingCodeError);
			}
		}
		if(page==='facebookSound'){
			$(setupCancelButton).unbind(click).click(function(){shrimp7.settings.setupPage('selectServices',FALSE)});
			$(setupNextButton).unbind(click).click(function(){shrimp7.settings.setupPage('facebookSound',TRUE)});
			
			if(second){
				setting("boolFacebookSound",$('#boolFacebookSound',activeDom).is(':checked'));
				
				if(setting("boolTwitter"))
					shrimp7.settings.setupPage('twitterCode',FALSE);
				else
					shrimp7.settings.setupPage('finish',FALSE);
			}
		}
		if(page==='twitterCode'){
			if(setting("boolFacebook"))
				$(setupCancelButton).unbind(click).click(function(){shrimp7.settings.setupPage('facebookSound',FALSE)});
			else
				$(setupCancelButton).unbind(click).click(function(){shrimp7.settings.setupPage('selectServices',FALSE)});
			
			$(setupNextButton).unbind(click).click(function(){shrimp7.settings.setupPage('twitterCode',TRUE)});
			
			if(second){
				if($("#twitterPIN",activeDom).val()===""){
					$("#error_twitterCode",activeDom).html('Twitter one-time code is required').fadeIn();
				}else{
					shrimp7.settings.setupPage('twitterCheckCode',FALSE);
				}
			}
		}
		if(page==='twitterCheckCode'){
			$(setupCancelButton).unbind(click).click(function(){shrimp7.settings.setupPage('twitterCode',FALSE)}).hide();
			$(setupNextButton).unbind(click).click(function(){shrimp7.settings.setupPage('twitterSound',FALSE)}).hide();
			
			if(setting("twitterPIN")!==$('#twitterPIN',activeDom).val()){
				setting("twitterPIN",$('#twitterPIN',activeDom).val());
				// set access token naar nix..
				setting("twitterOauthTokenAccess","");
				$("#verifyingTwitterPIN",activeDom).html(htmlImgIconsFront+'loading_dots.gif"alt=""/>');
				shrimp7.twitter.con.requestAccessToken(function(data){
					if(data!==errorTxt){
						$(setupNextButton).show();
						$("#verifyingTwitterPIN",activeDom).html(language.settings.lngSettingsTwitterVerifyingCodeOK);
					}else{
						$(setupCancelButton).show();
						$("#verifyingTwitterPIN",activeDom).html(language.settings.lngSettingsTwitterVerifyingCodeError);
					}
				});
			}else{
				if(setting("twitterOauthTokenAccess")!==''){
					$(setupNextButton).show();
					$("#verifyingTwitterPIN",activeDom).html(language.settings.lngSettingsTwitterVerifyingCodeOK);
				}else{
					$(setupCancelButton).show();
					$("#verifyingTwitterPIN",activeDom).html(language.settings.lngSettingsTwitterVerifyingCodeError);
				}
			}
		}
		if(page==='twitterSound'){
			$(setupCancelButton).unbind(click).click(function(){shrimp7.settings.setupPage('selectServices',FALSE)});
			$(setupNextButton).unbind(click).click(function(){shrimp7.settings.setupPage('twitterSound',TRUE)});
			
			if(second){
				setting("boolTwitterSound",$('#boolTwitterSound',activeDom).is(':checked'));
				shrimp7.settings.setupPage('twitterDeveloper',FALSE);
			}
		}
		if(page==='twitterDeveloper'){
			$(setupCancelButton).unbind(click).click(function(){shrimp7.settings.setupPage('twitterSound',FALSE)});
			$(setupNextButton).unbind(click).click(function(){shrimp7.settings.setupPage('twitterDeveloper',TRUE)});
			
			if(second){
				setting("boolTwitterDeveloper",$('#boolTwitterDeveloper',activeDom).is(':checked'));
				if(setting("boolTwitterDeveloper")&&setting("boolTwitterDeveloperAdded")===""){
					shrimp7.twitter.con.createFriend('soupshrimp',function(){});
				}
				shrimp7.settings.setupPage('twitterTest',FALSE);
			}
		}
		if(page==='twitterTest'){
			$(setupCancelButton).unbind(click).click(function(){shrimp7.settings.setupPage('twitterDeveloper',FALSE)});
			$(setupNextButton).unbind(click).click(function(){shrimp7.settings.setupPage('twitterTest',TRUE)});
			
			if(second){
				if($('#boolTwitterTestTweet',activeDom).is(':checked')){
					setting("boolTwitterTestTweet",TRUE);
					shrimp7.twitter.con.newTweet({
						tweet:language.settings.lngSettingsTwitterTestTweet,
						callback:function(){
						
						}
					});
				}
				shrimp7.settings.setupPage('finish',TRUE);
			}
		}
		if(page==='finish'){
			$(setupCancelButton).hide();
			$(setupNextButton).unbind(click).click(function(){shrimp7.fn.hideFlyout();}).val('close');
			/*
			* Twitter init
			*/
			if(setting("boolTwitter")){
				if(typeof(this.setupParams.twitterInit==='function'))
					this.setupParams.twitterInit();
			}else{
				stopTimer('twitter');
			}
			/*
			* Facebook init
			*/
			if(setting("boolFacebook"))
				// Facebook init
				if(typeof(this.setupParams.twitterInit==='function')){
					this.setupParams.facebookInit();
				}
			else{
				stopTimer('facebook');
			}
		}
	}
};

